﻿namespace BaseClass.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoDPhieuDatHang
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private long _deid;
        private int _deiddangnhap;
        private int _deidnhacungcap;
        private int _desophieu;
        private string _sghichu;
        private string _sngay;
        private string _sngaycapnhat;
        private string _sngaygiao;
        private string _sphuongthucgiaohang;
        private string _sphuongthucthanhtoan;
        private string _sSchema;
        private string _stenmaytinh;

        public dtoDPhieuDatHang()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoDPhieuDatHang(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDPhieuDatHang.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand("delete from " + this._sSchema + "." + tableName + " where 1=1  ", connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDPhieuDatHang.cs");
                return false;
            }
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoDPhieuDatHang.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = Convert.ToInt32(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoDPhieuDatHang.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDPhieuDatHang.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnSOPHIEU, ",", columnNGAY, ",", columnNGAYGIAO, ",", columnIDNHACUNGCAP, ",", columnTENMAYTINH, 
                    ",", columnNGAYCAPNHAT, ",", columnIDDANGNHAP, ",", columnPHUONGTHUCGIAOHANG, ",", columnPHUONGTHUCTHANHTOAN, ",", columnGHICHU, ") values(", this._deid, ",", this._desophieu, ",to_timestamp('", this._sngay, 
                    "','dd/mm/yyyy hh24:mi'),to_timestamp('", this._sngaygiao, "','dd/mm/yyyy hh24:mi'),", this._deidnhacungcap, ",:", columnTENMAYTINH, ",now(),", this._deiddangnhap, ",:", columnPHUONGTHUCGIAOHANG, ",:", columnPHUONGTHUCTHANHTOAN, ",:", columnGHICHU, ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnPHUONGTHUCGIAOHANG, NpgsqlDbType.Text, 200).Value = this._sphuongthucgiaohang;
                command.Parameters.Add(columnPHUONGTHUCTHANHTOAN, NpgsqlDbType.Text, 200).Value = this._sphuongthucthanhtoan;
                command.Parameters.Add(columnGHICHU, NpgsqlDbType.Text, 200).Value = this._sghichu;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDPhieuDatHang.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0L;
            this._desophieu = 0;
            this._sngay = "";
            this._sngaygiao = "";
            this._deidnhacungcap = 0;
            this._stenmaytinh = "";
            this._sngaycapnhat = "";
            this._deiddangnhap = 0;
            this._sphuongthucgiaohang = "";
            this._sphuongthucthanhtoan = "";
            this._sghichu = "";
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoDPhieuDatHang.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnID, ",", columnSOPHIEU, ",to_char(", columnNGAY, ",'dd/mm/yyyy hh24:mi') as ", columnNGAY, ",to_char(", columnNGAYGIAO, ",'dd/mm/yyyy hh24:mi') as ", columnNGAYGIAO, ",", columnIDNHACUNGCAP, ",", columnTENMAYTINH, 
                    ",to_char(", columnNGAYCAPNHAT, ",'dd/mm/yyyy hh24:mi:ss') as ", columnNGAYCAPNHAT, ",", columnIDDANGNHAP, ",", columnPHUONGTHUCGIAOHANG, ",", columnPHUONGTHUCTHANHTOAN, ",", columnGHICHU, " from ", this._sSchema, ".", tableName, 
                    " where ", columnID, " = ", this._deid
                 }), connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._deid = Convert.ToInt64(dataTable.Rows[0][columnID].ToString());
                    this._desophieu = Convert.ToInt32(dataTable.Rows[0][columnSOPHIEU].ToString());
                    this._sngay = dataTable.Rows[0][columnNGAY].ToString();
                    this._sngaygiao = dataTable.Rows[0][columnNGAYGIAO].ToString();
                    this._deidnhacungcap = Convert.ToInt32(dataTable.Rows[0][columnIDNHACUNGCAP].ToString());
                    this._stenmaytinh = dataTable.Rows[0][columnTENMAYTINH].ToString();
                    this._sngaycapnhat = dataTable.Rows[0][columnNGAYCAPNHAT].ToString();
                    this._deiddangnhap = Convert.ToInt32(dataTable.Rows[0][columnIDDANGNHAP].ToString());
                    this._sphuongthucgiaohang = dataTable.Rows[0][columnPHUONGTHUCGIAOHANG].ToString();
                    this._sphuongthucthanhtoan = dataTable.Rows[0][columnPHUONGTHUCTHANHTOAN].ToString();
                    this._sghichu = dataTable.Rows[0][columnGHICHU].ToString();
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoDPhieuDatHang.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoDPhieuDatHang.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnSOPHIEU + ",to_char(" + columnNGAY + ",'dd/mm/yyyy hh24:mi') as " + columnNGAY + ",to_char(" + columnNGAYGIAO + ",'dd/mm/yyyy hh24:mi') as " + columnNGAYGIAO + "," + columnIDNHACUNGCAP + "," + columnTENMAYTINH + ",to_char(" + columnNGAYCAPNHAT + ",'dd/mm/yyyy hh24:mi:ss') as " + columnNGAYCAPNHAT + "," + columnIDDANGNHAP + "," + columnPHUONGTHUCGIAOHANG + "," + columnPHUONGTHUCTHANHTOAN + "," + columnGHICHU + " from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoDPhieuDatHang.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDPhieuDatHang.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnSOPHIEU, " = ", this._desophieu, ",", columnNGAY, " = to_timestamp('", this._sngay, "','dd/mm/yyyy hh24:mi'),", columnNGAYGIAO, " = to_timestamp('", this._sngaygiao, 
                    "','dd/mm/yyyy hh24:mi'),", columnIDNHACUNGCAP, " = ", this._deidnhacungcap, ",", columnTENMAYTINH, " = :", columnTENMAYTINH, ",", columnNGAYCAPNHAT, " = now(),", columnIDDANGNHAP, " = ", this._deiddangnhap, ",", columnPHUONGTHUCGIAOHANG, 
                    " = :", columnPHUONGTHUCGIAOHANG, ",", columnPHUONGTHUCTHANHTOAN, " = :", columnPHUONGTHUCTHANHTOAN, ",", columnGHICHU, " = :", columnGHICHU, " where ", columnID, " = ", this._deid
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnPHUONGTHUCGIAOHANG, NpgsqlDbType.Text, 200).Value = this._sphuongthucgiaohang;
                command.Parameters.Add(columnPHUONGTHUCTHANHTOAN, NpgsqlDbType.Text, 200).Value = this._sphuongthucthanhtoan;
                command.Parameters.Add(columnGHICHU, NpgsqlDbType.Text, 200).Value = this._sghichu;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDPhieuDatHang.cs");
                return false;
            }
        }

        public static string columnGHICHU
        {
            get
            {
                return "ghichu";
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnIDDANGNHAP
        {
            get
            {
                return "iddangnhap";
            }
        }

        public static string columnIDNHACUNGCAP
        {
            get
            {
                return "idnhacungcap";
            }
        }

        public static string columnNGAY
        {
            get
            {
                return "ngay";
            }
        }

        public static string columnNGAYCAPNHAT
        {
            get
            {
                return "ngaycapnhat";
            }
        }

        public static string columnNGAYGIAO
        {
            get
            {
                return "ngaygiao";
            }
        }

        public static string columnPHUONGTHUCGIAOHANG
        {
            get
            {
                return "phuongthucgiaohang";
            }
        }

        public static string columnPHUONGTHUCTHANHTOAN
        {
            get
            {
                return "phuongthucthanhtoan";
            }
        }

        public static string columnSOPHIEU
        {
            get
            {
                return "sophieu";
            }
        }

        public static string columnTENMAYTINH
        {
            get
            {
                return "tenmaytinh";
            }
        }

        public string pGHICHU
        {
            get
            {
                return this._sghichu;
            }
            set
            {
                this._sghichu = value;
            }
        }

        public long pID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public int pIDDANGNHAP
        {
            get
            {
                return this._deiddangnhap;
            }
            set
            {
                this._deiddangnhap = value;
            }
        }

        public int pIDNHACUNGCAP
        {
            get
            {
                return this._deidnhacungcap;
            }
            set
            {
                this._deidnhacungcap = value;
            }
        }

        public string pNGAY
        {
            get
            {
                return this._sngay;
            }
            set
            {
                this._sngay = value;
            }
        }

        public string pNGAYCAPNHAT
        {
            get
            {
                return this._sngaycapnhat;
            }
            set
            {
                this._sngaycapnhat = value;
            }
        }

        public string pNGAYGIAO
        {
            get
            {
                return this._sngaygiao;
            }
            set
            {
                this._sngaygiao = value;
            }
        }

        public string pPHUONGTHUCGIAOHANG
        {
            get
            {
                return this._sphuongthucgiaohang;
            }
            set
            {
                this._sphuongthucgiaohang = value;
            }
        }

        public string pPHUONGTHUCTHANHTOAN
        {
            get
            {
                return this._sphuongthucthanhtoan;
            }
            set
            {
                this._sphuongthucthanhtoan = value;
            }
        }

        public int pSOPHIEU
        {
            get
            {
                return this._desophieu;
            }
            set
            {
                this._desophieu = value;
            }
        }

        public string pTENMAYTINH
        {
            get
            {
                return this._stenmaytinh;
            }
            set
            {
                this._stenmaytinh = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "d_phieudathang";
            }
        }
    }
}

